﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports ISNet.WebUI.WebGrid
Imports System.Data
Imports ISNet.WebUI.WebTreeView
Imports log4net

Partial Class ActTypeProject
    Inherits System.Web.UI.Page
    'Dim cmmtcode As String = ""
    Public price As String = ""
    Public CodeTxt As String = ""
    Dim pricehash As New Hashtable
    Dim cmmthash As New Hashtable
    Dim pubprj As New Hashtable
    Dim lockhash As New Hashtable
    Public dfctr As String

    Private Shared logger As ILog = LogManager.GetLogger("ActTypeProject")

    Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
        WebCombo3.EncodeOutput = True
    End Sub

    Sub setDefault()
        If Session("dfctr") IsNot Nothing Then
            dfctr = "dfc= '" & Session("dfctr") & "'; dfcname = '" & Session("dfctrname") & "';"
        Else
            dfctr = "dfc = 'null'"
        End If
    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        'cmmtcode = Session("CMMT_OT_CODE")
        PlanYearsTxt.Text = Session("PlanYear")
        VersionTxt.Text = Session("Versions")
        If Session("FCTR") Is Nothing Then
            Response.Redirect("Selector.aspx?type=7" & "&ptype=" & Request("ptype"))
        End If
        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If
        
        GenerateValues()
        setDefault()
        If IsPostBack = False Then
            Session.Remove("Search")
            Session.Remove("search_fctr")
            Session.Remove("search_activitytype")

            If Request("ptype") = "P" Then
                grid_name.InnerHtml = "<strong>ครุภัณฑ์-โครงการ</strong>"
                fil_name.InnerHtml = "<strong>&nbsp;โครงการ</strong>"
                Label1.Text = "ระบบจัดทำงบประมาณ >> จัดทำงบประมาณ >> ครุภัณฑ์น >> ครุภัณฑ์-งานโครงการ"
            Else
                grid_name.InnerHtml = "<strong>ครุภัณฑ์-งานสนับสนุน</strong>"
                fil_name.InnerHtml = "<strong>&nbsp;งานสนับสนุน</strong>"
                Label1.Text = "ระบบจัดทำงบประมาณ >> จัดทำงบประมาณ >> ครุภัณฑ์ >> ครุภัณฑ์-งานสนับสนุน"
            End If

            'If Session("Versions") = 1 Then
            '    div1.Visible = False
            '    div2.Visible = False
            '    SearhBtn.Visible = False
            '    WebTreeView1.Visible = False
            '    WebTreeView2.Visible = False
            '    WebTreeView3.Visible = False
            'End If
            Dim adapter As New AttachmentTableAdapters.ProjectsTableAdapter
            Dim data As New Attachment.ProjectsDataTable
            'data = adapter.GetDataByPlanYear(Val(Session("PlanYear")))
            Dim sql = ""
            Dim prefil As String = ""
            prefil = Session("prefilter")
            sql += " WHERE 1 = 1 "
            sql += " AND PlanYear = " & PlanYearsTxt.Text & " "
            If prefil <> "" Then
                sql += " AND FundCenter_FundCenterCode IN (" & prefil & ") "
            End If
            sql += " AND LEFT(ProjectCode,1) = '" & Request("ptype") & "' "
            sql += " UNION  "
            sql += " SELECT ProjectCode, PlanYear, (Description) AS Description , CreatedDate, UpdatedDate, CreatedBy, UpdatedBy, FundCenter_FundCenterCode "
            sql += " FROM Projects AS Projects1 "
            sql += " INNER JOIN ProjectFundCenters AS ProjectFundCenters1 ON(Projects1.ProjectCode = ProjectFundCenters1.ProjectCode AND Projects1.PlanYear = ProjectFundCenters1.PlanYear)"
            sql += " WHERE Projects1.FundCenter_FundCenterCode IS NULL "
            sql += " AND Projects1.PlanYear = " & PlanYearsTxt.Text & " "
            sql += " AND LEFT(Projects1.ProjectCode,1) = '" & Request("ptype") & "' "
            If prefil <> "" Then
                prefil += " AND CONVERT(INT,Fcth.FundCenter_FundCenterCode) BETWEEN CONVERT(INT,ProjectFundCenters1.FromFundCenter_FundCenterCode) AND CONVERT(INT,ProjectFundCenters1.TOFundCenter_FundCenterCode) "
                sql += "  AND EXISTS (" & prefil & ") "
            End If
            adapter.GetDataByFilter(data, sql)

            ms8.DataValueField = "ProjectCode"
            ms8.DataTextField = "Description"
            ms8.DataSource = data

            ms8.DataBind()

        End If
        If WebGrid1.IsFlyPostBack = False Then
            Dim adap2 As New BasicDataTableAdapters.ActivityTypesTableAdapter
            Dim dt2 As New BasicData.ActivityTypesDataTable
            dt2 = adap2.GetDataByPlanYear(Val(Session("PlanYear")))
            Dim i As Integer = 0
            'cmt_hash.Clear()
            For Each row As BasicData.ActivityTypesRow In dt2
                price += "CodeArray[" & i & "]='" & row.ActivityTypeCode & "';"
                price += "Price[" & i & "]='" & row.Price & "';"
                i += 1
                pricehash.Remove(row.ActivityTypeCode)
                pricehash.Add(row.ActivityTypeCode, row.Price)
            Next



            'PlotNode()
        End If
        'Dim adap As New BasicDataTableAdapters.Projects1TableAdapter
        'Dim dt3 As New BasicData.Projects1DataTable
        'dt3 = adap.GetDataByFCTRnull(Val(Session("PlanYear")), Request("ptype"))
        'For Each item As BasicData.Projects1Row In dt3
        '    If item.IsFundCenterCodeNull = True Then
        '        pubprj.Remove(item.ProjectCode)
        '        pubprj.Add(item.ProjectCode, item.ProjectCode)
        '    End If
        'Next

        Dim prefil2 As String = ""
        prefil2 = Session("prefilter")
        Dim re_adap As New FormLockDSTableAdapters.FundCenterReleasesTableAdapter
        Dim rsql As String = ""
        rsql = " SELECT FundCenterReleases.FundCenter_FundCenterCode FROM FundCenterReleases "
        rsql += " WHERE PlanYear = " & Val(PlanYearsTxt.Text) & " "
        rsql += " AND Version = '" & VersionTxt.Text & "' "
        rsql += " AND FundCenter_FundCenterCode IN (" & prefil2 & ")" : rsql += " AND ReleaseFlag = 'Y' "
        'Response.Write(rsql)
        'Exit Sub
        Dim re_dt As New FormLockDS.FundCenterReleasesDataTable
        re_adap.GetDataByFilter(re_dt, rsql)
        For Each ritem As FormLockDS.FundCenterReleasesRow In re_dt
            lockhash.Remove(ritem.FundCenter_FundCenterCode) : lockhash.Add(ritem.FundCenter_FundCenterCode, True)
        Next

        Dim cmmtadap As New ActTypeDSTableAdapters.ASSET_CMMTTableAdapter
        Dim cmmtdt As New ActTypeDS.ASSET_CMMTDataTable
        cmmtdt = cmmtadap.GetData(Val(PlanYearsTxt.Text))
        For Each row2 As ActTypeDS.ASSET_CMMTRow In cmmtdt
            cmmthash.Remove(row2.Name)
            cmmthash.Add(row2.Name, row2.Value)
        Next
    End Sub

    Protected Sub WebGrid1_InitializeDataSource(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.DataSourceEventArgs) Handles WebGrid1.InitializeDataSource
        Dim adapter As New ActTypeDSTableAdapters.ActTypePrj2TableAdapter
        Dim datatable As New ActTypeDS.ActTypePrj2DataTable
        Dim prefil As String = ""
        prefil = Session("prefilter")

        'If Session("Versions") > 1 Then
        If IsPostBack = True And Session("Search") = "true" Then
            Dim sql As String = ""
            Dim fctr As String = ""
            fctr = Session("search_fctr")
            Dim search_activitytype As String = ""
            search_activitytype = Session("search_activitytype")
            If TabContainer1.ActiveTab.ID = TabPanel1.ID Then
                sql += " WHERE 1 = 1 "
                'sql += " WHERE BudgetDetails.FundCenter_FundCenterCode BETWEEN " & Val(WebCombo1.Value) & " AND " & Val(WebCombo2.Value

                If hdf1.Value.ToString <> "" Then
                    sql += " AND BudgetDetails.Project_ProjectCode IN ("
                    Dim Numx As Array = hdf1.Value.Split(",")
                    For i = 0 To Numx.Length - 1
                        If i <> 0 Then
                            sql += ","
                        End If
                        sql += "'" & Numx(i).ToString & "'"

                    Next
                    sql += ") "
                End If
                sql += " AND BudgetDetails.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND Version = '" & VersionTxt.Text & "'"
                sql += " AND FormId = 'F04' "
                sql += " AND BudgetDetails.Project_ProjectCode IS NOT NULL "
                If fctr <> "" Then
                    If fctr <> "0" Then
                        sql += " AND BudgetDetails.FundCenter_FundCenterCode IN (" & fctr & ") "
                    Else
                        sql += " AND BudgetDetails.FundCenter_FundCenterCode IN (" & prefil & ") "
                    End If
                Else
                    sql += " AND BudgetDetails.FundCenter_FundCenterCode IN (" & prefil & ") "
                End If
                sql += " AND LEFT(BudgetDetails.Project_ProjectCode,1) = '" & Request("ptype") & "' "
                sql += " AND Projects.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND FundCenters.PlanYear = " & PlanYearsTxt.Text & " "

                If search_activitytype IsNot Nothing And search_activitytype <> "" Then
                    sql += search_activitytype

                Else
                    sql += " UNION "

                    sql += " SELECT BudgetDetails.Id, BudgetDetails.PlanYear, BaseYear, [Version] "
                    sql += " , NewQuantity, ReplaceQuantity, (NewQuantity + ReplaceQuantity) AS SumQuantity "
                    sql += " , PricePerUnit, Amount, FormId, Note, isnull(AssetNo,'') AS ActivityType_ActivityTypeCode "
                    sql += " , isnull(AssetDescription,'') AS ActivityTypeDesc , BudgetDetails.AssetRowNo AS AssetGroup, OldAssetNo "
                    sql += " , BudgetDetails.Project_ProjectCode, BudgetDetails.FundCenter_FundCenterCode "
                    sql += " , Attachments_amount "
                    sql += " ,Projects.[Description] AS ProjectDes, FundCenters.Description AS FundCenterName "
                    sql += " ,'' "
                    sql += " FROM BudgetDetails "
                    sql += " INNER JOIN Projects ON BudgetDetails.Project_ProjectCode = Projects.ProjectCode "
                    sql += " AND (Projects.FundCenter_FundCenterCode = BudgetDetails.FundCenter_FundCenterCode OR Projects.FundCenter_FundCenterCode IS NULL) "
                    sql += " INNER JOIN FundCenters ON BudgetDetails.FundCenter_FundCenterCode = FundCenters.FundCenterCode "
                    sql += " WHERE FormId = 'F04' AND BudgetDetails.ActivityType_ActivityTypeCode IS NULL "

                    If hdf1.Value.ToString <> "" Then
                        sql += " AND BudgetDetails.Project_ProjectCode IN ("
                        Dim Numx As Array = hdf1.Value.Split(",")
                        For i = 0 To Numx.Length - 1
                            If i <> 0 Then
                                sql += ","
                            End If
                            sql += "'" & Numx(i).ToString & "'"

                        Next
                        sql += ") "
                    End If
                    sql += " AND BudgetDetails.PlanYear = " & PlanYearsTxt.Text & " "
                    sql += " AND Version = '" & VersionTxt.Text & "'"
                    sql += " AND FormId = 'F04' "
                    sql += " AND BudgetDetails.Project_ProjectCode IS NOT NULL "
                    If fctr <> "" Then
                        If fctr <> "0" Then
                            sql += " AND BudgetDetails.FundCenter_FundCenterCode IN (" & fctr & ") "
                        Else
                            sql += " AND BudgetDetails.FundCenter_FundCenterCode IN (" & prefil & ") "
                        End If
                    Else
                        sql += " AND BudgetDetails.FundCenter_FundCenterCode IN (" & prefil & ") "
                    End If
                    sql += " AND LEFT(BudgetDetails.Project_ProjectCode,1) = '" & Request("ptype") & "' "
                    sql += " AND Projects.PlanYear = " & PlanYearsTxt.Text & " "
                    sql += " AND FundCenters.PlanYear = " & PlanYearsTxt.Text & " "
                End If

                sql += " ORDER BY FundCenter_FundCenterCode,Id"
                adapter.GetDataByFilter(datatable, sql)

            ElseIf TabContainer1.ActiveTab.ID = TabPanel2.ID Then
                sql += " WHERE 1 = 1 "
                'sql += " WHERE BudgetDetails.FundCenter_FundCenterCode IN (SELECT FundCenter_FundCenterCode FROM FundCenterHierarchies "
                'sql += " WHERE FundCenter_FundCenterCode = " & Val(WebCombo3.Value) & " OR ParentFundCenterHierarchy_Id = " & Val(WebCombo3.Value) & ") "
                If hdf1.Value.ToString <> "" Then
                    sql += " AND BudgetDetails.Project_ProjectCode IN ("
                    Dim Numx As Array = hdf1.Value.Split(",")
                    For i = 0 To Numx.Length - 1
                        If i <> 0 Then
                            sql += ","
                        End If
                        sql += "'" & Numx(i).ToString & "'"

                    Next
                    sql += ") "
                End If
                sql += " AND BudgetDetails.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND Version = '" & VersionTxt.Text & "'"
                sql += " AND FormId = 'F04' "
                sql += " AND BudgetDetails.Project_ProjectCode IS NOT NULL "
                If fctr <> "" Then
                    sql += " AND BudgetDetails.FundCenter_FundCenterCode IN (" & fctr & ") "
                Else
                    sql += " AND BudgetDetails.FundCenter_FundCenterCode IN (" & prefil & ") "
                End If
                sql += " AND LEFT(BudgetDetails.Project_ProjectCode,1) = '" & Request("ptype") & "' "
                sql += " AND Projects.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND FundCenters.PlanYear = " & PlanYearsTxt.Text & " "

                If search_activitytype IsNot Nothing And search_activitytype <> "" Then
                    sql += search_activitytype
                Else
                    sql += " UNION "

                    sql += " SELECT BudgetDetails.Id, BudgetDetails.PlanYear, BaseYear, [Version] "
                    sql += " , NewQuantity, ReplaceQuantity, (NewQuantity + ReplaceQuantity) AS SumQuantity "
                    sql += " , PricePerUnit, Amount, FormId, Note, isnull(AssetNo,'') AS ActivityType_ActivityTypeCode "
                    sql += " , isnull(AssetDescription,'') AS ActivityTypeDesc , CONVERT(nvarchar(2), BudgetDetails.AssetRowNo) AS AssetGroup, OldAssetNo "
                    sql += " , BudgetDetails.Project_ProjectCode, BudgetDetails.FundCenter_FundCenterCode "
                    sql += " , Attachments_amount "
                    sql += " ,Projects.[Description] AS ProjectDes, FundCenters.Description AS FundCenterName "
                    sql += " ,'' "
                    sql += " FROM BudgetDetails "
                    sql += " INNER JOIN Projects ON BudgetDetails.Project_ProjectCode = Projects.ProjectCode "
                    sql += " AND (Projects.FundCenter_FundCenterCode = BudgetDetails.FundCenter_FundCenterCode OR Projects.FundCenter_FundCenterCode IS NULL) "
                    sql += " INNER JOIN FundCenters ON BudgetDetails.FundCenter_FundCenterCode = FundCenters.FundCenterCode "
                    sql += " WHERE FormId = 'F04' AND BudgetDetails.ActivityType_ActivityTypeCode IS NULL "

                    If hdf1.Value.ToString <> "" Then
                        sql += " AND BudgetDetails.Project_ProjectCode IN ("
                        Dim Numx As Array = hdf1.Value.Split(",")
                        For i = 0 To Numx.Length - 1
                            If i <> 0 Then
                                sql += ","
                            End If
                            sql += "'" & Numx(i).ToString & "'"

                        Next
                        sql += ") "
                    End If
                    sql += " AND BudgetDetails.PlanYear = " & PlanYearsTxt.Text & " "
                    sql += " AND Version = '" & VersionTxt.Text & "'"
                    sql += " AND FormId = 'F04' "
                    sql += " AND BudgetDetails.Project_ProjectCode IS NOT NULL "
                    If fctr <> "" Then
                        sql += " AND BudgetDetails.FundCenter_FundCenterCode IN (" & fctr & ") "
                    Else
                        sql += " AND BudgetDetails.FundCenter_FundCenterCode IN (" & prefil & ") "
                    End If
                    sql += " AND LEFT(BudgetDetails.Project_ProjectCode,1) = '" & Request("ptype") & "' "
                    sql += " AND Projects.PlanYear = " & PlanYearsTxt.Text & " "
                    sql += " AND FundCenters.PlanYear = " & PlanYearsTxt.Text & " "
                End If

                sql += " ORDER BY FundCenter_FundCenterCode,Id"
                adapter.GetDataByFilter(datatable, sql)
                'Response.Write(sql)

                End If

                'Response.Write(sql)
                datatable.Columns("PlanYear").AllowDBNull = True
                datatable.Columns("BaseYear").AllowDBNull = True
                datatable.Columns("Version").AllowDBNull = True
                datatable.Columns("FundCenter_FundCenterCode").AllowDBNull = True
                'datatable.Columns("FundCenter_PlanYear").AllowDBNull = True
                'datatable.Columns("Fund_FundCode").AllowDBNull = True
                'datatable.Columns("Fund_PlanYear").AllowDBNull = True

                datatable.Columns("Id").ReadOnly = False
                datatable.Columns("SumQuantity").ReadOnly = False

                datatable.Columns.Add("SumEl", Type.GetType("System.Decimal"))
                'Response.Write(Val(Session("FCTR")))
                e.DataSource = datatable


                'datatable = adapter.GetDataByMultiple(Val(Session("PlanYear")), Val(Session("Versions")), Session("users"))
            Else
                Dim sql As String = ""

                sql += " WHERE 1 = 1 "
                'sql += " WHERE BudgetDetails.FundCenter_FundCenterCode BETWEEN " & Val(WebCombo1.Value) & " AND " & Val(WebCombo2.Value)
                sql += " AND BudgetDetails.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND Version = '" & VersionTxt.Text & "'"
                sql += " AND FormId = 'F04' "
                sql += " AND BudgetDetails.Project_ProjectCode IS NOT NULL "
                If prefil <> "" Then
                    sql += " AND BudgetDetails.FundCenter_FundCenterCode IN (" & prefil & ") "
                End If
                sql += " AND LEFT(BudgetDetails.Project_ProjectCode,1) = '" & Request("ptype") & "' "
                sql += " AND Projects.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND FundCenters.PlanYear = " & PlanYearsTxt.Text & " "

                sql += " UNION "

                sql += " SELECT BudgetDetails.Id, BudgetDetails.PlanYear, BaseYear, [Version] "
                sql += " , NewQuantity, ReplaceQuantity, (NewQuantity + ReplaceQuantity) AS SumQuantity "
                sql += " , PricePerUnit, Amount, FormId, Note, isnull(AssetNo,'') AS ActivityType_ActivityTypeCode "
                sql += " , isnull(AssetDescription,'') AS ActivityTypeDesc , BudgetDetails.AssetRowNo AS AssetGroup, OldAssetNo "
                sql += " , BudgetDetails.Project_ProjectCode, BudgetDetails.FundCenter_FundCenterCode "
                sql += " , Attachments_amount "
                sql += " ,Projects.[Description] AS ProjectDes, FundCenters.Description AS FundCenterName "
                sql += " ,'' "
                sql += " FROM BudgetDetails "
                sql += " INNER JOIN Projects ON BudgetDetails.Project_ProjectCode = Projects.ProjectCode "
                sql += " AND (Projects.FundCenter_FundCenterCode = BudgetDetails.FundCenter_FundCenterCode OR Projects.FundCenter_FundCenterCode IS NULL) "
                sql += " INNER JOIN FundCenters ON BudgetDetails.FundCenter_FundCenterCode = FundCenters.FundCenterCode "
                sql += " WHERE FormId = 'F04' AND BudgetDetails.ActivityType_ActivityTypeCode IS NULL "

                sql += " AND BudgetDetails.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND Version = '" & VersionTxt.Text & "'"
                sql += " AND FormId = 'F04' "
                sql += " AND BudgetDetails.Project_ProjectCode IS NOT NULL "
                If prefil <> "" Then
                    sql += " AND BudgetDetails.FundCenter_FundCenterCode IN (" & prefil & ") "
                End If
                sql += " AND LEFT(BudgetDetails.Project_ProjectCode,1) = '" & Request("ptype") & "' "
                sql += " AND Projects.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND FundCenters.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " ORDER BY FundCenter_FundCenterCode,Id"
                adapter.GetDataByFilter(datatable, sql)
                'Response.Write(sql)
                'datatable = adapter.GetDataByMultiple2(Val(Session("PlanYear")), Val(Session("Versions")), Session("users"))

                datatable.Columns("PlanYear").AllowDBNull = True
                datatable.Columns("BaseYear").AllowDBNull = True
                datatable.Columns("Version").AllowDBNull = True
                datatable.Columns("FundCenter_FundCenterCode").AllowDBNull = True
                'datatable.Columns("FundCenter_PlanYear").AllowDBNull = True
                'datatable.Columns("Fund_FundCode").AllowDBNull = True
                'datatable.Columns("Fund_PlanYear").AllowDBNull = True

                datatable.Columns("Id").ReadOnly = False
                datatable.Columns("SumQuantity").ReadOnly = False

                datatable.Columns.Add("SumEl", Type.GetType("System.Decimal"))
                'Response.Write(Val(Session("FCTR")))
                e.DataSource = datatable

            End If


    End Sub

    Protected Sub WebGrid1_InitializeRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        ' Dim area_level As Decimal = Val(e.Row.Cells.GetNamedItem("area_level").Text)
        'If area_level = 9 Then
        'e.Row.Style.BackColor = Drawing.Color.Coral
        ' End If
        e.Row.Cells(8).ForceNoEdit = True
        e.Row.Cells(14).ForceNoEdit = True
        e.Row.Cells(15).ForceNoEdit = True

        If e.Row.Type = RowType.Record Then
            Dim cells As WebGridCellCollection = e.Row.Cells
            Dim dr As DataRowView = DirectCast(e.Row.DataRow, DataRowView)
            Dim code As String = dr("ActivityType_ActivityTypeCode").ToString()
            If code.Trim.Remove(1) = "9" Then
                e.Row.Cells(9).ForceNoEdit = False
            Else
                e.Row.Cells(9).ForceNoEdit = True
            End If

            If pricehash(code) Is Nothing Or pricehash(code) = 0 Then
                e.Row.Cells(9).ForceNoEdit = False
            End If

            Dim num1 As Decimal = dr("NewQuantity")
            Dim num2 As Decimal = dr("ReplaceQuantity")
            Dim num3 As Decimal = dr("PricePerUnit")


            e.Row.Cells.GetNamedItem("SumEl").Text = Format(((num1 * num3) + (num2 * num3)), "###,###.00")
            e.Row.Cells.GetNamedItem("SumEl").Value = Format(((num1 * num3) + (num2 * num3)), "###,###.00")
            e.Row.Cells.GetNamedItem("SumEl").ForceNoEdit = True
            dr("SumEl") = (num1 * num3) + (num2 * num3)
        End If

        If e.Row.Type = RowType.Record Then
            If lockhash(e.Row.Cells(1).Value.ToString.Trim) = True Then
                e.Row.Cells(0).Column.IsRowChecker = False
                e.Row.ForceNoEdit = True
                e.Row.Style.BackColor = Drawing.Color.Gray
            Else
                e.Row.Cells(0).Column.IsRowChecker = True
            End If
        End If
    End Sub


    Protected Sub FundCenterCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles FundCenterCombo.InitializeDataSource
        Dim ds As New DataSet
        'If Session("Versions") > 1 Then
        If IsPostBack = True And Session("Search") = "true" Then
            Dim fctr As String = ""
            fctr = Session("search_fctr")
            Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
            Dim dt As New BasicData.FCTR_Child2DataTable
            Dim sql = ""
            sql += " WHERE 1 = 1 "
            sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYear"))
            If fctr <> "" Then
                sql += " AND fctr_view.FundCenterCode IN (" & fctr & ") "
            End If
            sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("PlanYear"))
            sql += "ORDER BY [Level] ASC, FundCenterCode ASC"
            adap.GetDataByFilter(dt, sql)
            e.DataSource = dt
            'Response.Write(sql & "<br />")
        Else
            'ds = PublicFunction.GetChild2(Session("users"))
            'ds.Tables(0).DefaultView.Sort = "FundCenterCode ASC"
            'e.DataSource = ds
            Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
            Dim dt As New BasicData.FCTR_Child2DataTable
            Dim sql = ""
            Dim prefil As String = ""
            prefil = Session("prefilter")
            sql += " WHERE 1 = 1 "
            sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYear"))
            If prefil <> "" Then
                sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
            End If
            sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("PlanYear")) & " "
            sql += "ORDER BY [Level] ASC, FundCenterCode ASC"
            adap.GetDataByFilter(dt, sql)
            e.DataSource = dt
        End If


        'Else
        'Dim adap As New BasicDataTableAdapters.FCTR_ChildTableAdapter
        'Dim dt As New BasicData.FCTR_ChildDataTable
        'dt = adap.GetOwnData(Session("FCTR"))
        'e.DataSource = dt
        'End If
    End Sub

    Protected Sub AssetGroup_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles AssetGroupCombo.InitializeDataSource
        'Dim dt As New DataTable
        'dt.Columns.Add("AssetGroup")
        'dt.Columns.Add("Description")

        'dt.Rows.Add(1, "ครุภัณฑ์สำนักงาน")
        'dt.Rows.Add(2, "ครุภัณฑ์คอมพิวเตอร์")
        'dt.Rows.Add(3, "ครุภัณฑ์ขนส่ง")

        Dim adap As New DBdataTableAdapters.AssetGroupTableAdapter
        Dim dt As New DBdata.AssetGroupDataTable
        dt = adap.GetData

        e.DataSource = dt

    End Sub

    Protected Sub ProjectCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles ProjectCombo.InitializeDataSource
        Dim adap As New BasicDataTableAdapters.Projects1TableAdapter
        Dim dt As New BasicData.Projects1DataTable
        dt = adap.GetDataByPrefix(Val(Session("PlanYear")), Request("ptype"))

        'Dim dt3 As New BasicData.Projects1DataTable
        'dt3 = adap.GetDataByFCTRnull(Val(Session("PlanYear")), Request("ptype"))

        Dim prefil As String = Session("prefilter")
        Dim sql = ""
        sql += " INNER JOIN ProjectFundCenters AS ProjectFundCenters1 ON(Projects.ProjectCode = ProjectFundCenters1.ProjectCode AND Projects.PlanYear = ProjectFundCenters1.PlanYear)"

        sql += " WHERE 1 = 1 "

        sql += " AND Projects.FundCenter_FundCenterCode IS NULL "
        sql += " AND Projects.PlanYear = " & PlanYearsTxt.Text & " "
        sql += " AND LEFT(Projects.ProjectCode,1) = '" & Request("ptype") & "' "
        If prefil <> "" Then
            prefil += " AND CONVERT(INT,Fcth.FundCenter_FundCenterCode) BETWEEN CONVERT(INT,ProjectFundCenters1.FromFundCenter_FundCenterCode) AND CONVERT(INT,ProjectFundCenters1.TOFundCenter_FundCenterCode) "
            sql += "  AND EXISTS (" & prefil & ") "
        End If

        Dim dt3 As New BasicData.Projects1DataTable
        adap.GetDataByFilter(dt3, sql)

        prefil = Session("prefilter")
        Dim fadap As New FormLockDSTableAdapters.FundCentersTableAdapter
        Dim fdt As New FormLockDS.FundCentersDataTable
        Dim pfadp As New B10DataSetTableAdapters.ProjectFundCentersTableAdapter
        fadap.GetDataByFilter(fdt, prefil)
        For Each fitem As FormLockDS.FundCentersRow In fdt
            Dim isMerge = False
            For Each item As BasicData.Projects1Row In dt3
                Dim canUseProject = pfadp.CanUseProject(Session("PlanYear"), item.ProjectCode, fitem.FundCenter_FundCenterCode)
                If canUseProject <> 0 Then
                    isMerge = True
                    item.FundCenterCode = fitem.FundCenter_FundCenterCode
                End If
            Next
            If isMerge Then
                dt.Merge(dt3)
            End If
        Next

        e.DataSource = dt

    End Sub

    Protected Sub ActivityTypeCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles ActivityTypeCombo.InitializeDataSource, ActivityTypeCodeCombo.InitializeDataSource
        Dim adap As New BasicDataTableAdapters.ActivityTypesTableAdapter
        Dim dt As New BasicData.ActivityTypesDataTable
        dt = adap.GetDataByPlanYear(Val(Session("PlanYear")))
        e.DataSource = dt

    End Sub

    Protected Sub WebGrid1_AddRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        If lockhash(e.Row.Cells(1).Value.ToString.Trim) = True Then
            WebGrid1.ClearCachedDataSource()
            Throw New ArgumentException("ไม่สามารถบันทึกได้ เนื่องจากมีการส่งแผนไปแล้ว")
        End If

        Dim adapter As New BudgetDetailTableAdapters.BudgetDetailsTableAdapter

        Dim id As Decimal

        Dim padap As New BasicDataTableAdapters.ProjectsTableAdapter
        Dim pfctr As String = padap.GetFCTR(e.Row.Cells(2).Value, Val(PlanYearsTxt.Text))

        If e.Row.Cells(4).Text.ToString.Trim.Remove(1) = "9" Then
            Dim asg As Integer = Val(e.Row.Cells(3).Value)
            Dim cmmt_id As String = "0"
            Try
                Select Case asg
                    Case 1
                        cmmt_id = cmmthash("ASSET_GROUP1_CMMT_CODE")
                    Case 2
                        cmmt_id = cmmthash("ASSET_GROUP2_CMMT_CODE")
                    Case 3
                        cmmt_id = cmmthash("ASSET_GROUP3_CMMT_CODE")
                    Case 4
                        cmmt_id = cmmthash("ASSET_GROUP4_CMMT_CODE")
                    Case 5
                        cmmt_id = cmmthash("ASSET_GROUP5_CMMT_CODE")
                    Case 6
                        cmmt_id = cmmthash("ASSET_GROUP6_CMMT_CODE")
                    Case 7
                        cmmt_id = cmmthash("ASSET_GROUP7_CMMT_CODE")
                    Case 8
                        cmmt_id = cmmthash("ASSET_GROUP8_CMMT_CODE")
                    Case 9
                        cmmt_id = cmmthash("ASSET_GROUP9_CMMT_CODE")
                    Case 10
                        cmmt_id = cmmthash("ASSET_GROUP10_CMMT_CODE")

                End Select
            Catch ex As Exception
                logger.Error(ex.Message)
                logger.Error(ex.StackTrace)

                WebGrid1.ClearCachedDataSource()
                Throw New ArgumentException("ไม่สามารถบันทึกได้ เนื่องจากค่าในระบบไม่ถูกต้องโปรดติดต่อผู้ดูแลระบบ")
            End Try
            If cmmt_id = "0" Or cmmt_id Is Nothing Then
                WebGrid1.ClearCachedDataSource()
                Throw New ArgumentException("ไม่สามารถบันทึกได้ เนื่องจากค่าในระบบไม่ถูกต้องโปรดติดต่อผู้ดูแลระบบ")
            End If
            Dim adap3 As New ActTypeDSTableAdapters.BudgetDetailsTableAdapter
            Dim dup As Integer = 0
            dup = adap3.CheckAssetInsert(e.Row.Cells(4).Text.ToString.Trim, e.Row.Cells(1).Value, Val(PlanYearsTxt.Text), VersionTxt.Text)
            If dup = 0 Then
                Try
                    id = adapter.InsertQuery(Val(PlanYearsTxt.Text), Val(PlanYearsTxt.Text), VersionTxt.Text, e.Row.Cells(4).Text, e.Row.Cells(5).Text, Val(e.Row.Cells(3).Value), e.Row.Cells(12).Text.ToString, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Val(e.Row.Cells(11).Value), Val(e.Row.Cells(6).Value), Val(e.Row.Cells(7).Value), Val(e.Row.Cells(9).Value), e.Row.Cells(13).Value, "", "F04", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Date.Now, Date.Now, Session("users"), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, e.Row.Cells(1).Value, Val(PlanYearsTxt.Text), e.Row.Cells(2).Value, Val(PlanYearsTxt.Text), cmmt_id, Val(PlanYearsTxt.Text), Nothing, "2100", Val(PlanYearsTxt.Text), 0, If(pfctr = "null", Nothing, e.Row.Cells(1).Value))
                    logger.Info("User " & Session("users") & " insert activity type " & e.Row.Cells(4).Value & " of fund center " & e.Row.Cells(1).Value)
                Catch ex As Exception
                    logger.Error(ex.Message)
                    logger.Error(ex.StackTrace)

                    WebGrid1.ClearCachedDataSource()
                    Throw New ArgumentException("ไม่สามารถเพิ่มข้อมูลได้ กรุณาตรวจสอบจำนวนเงิน")
                End Try
            Else
                WebGrid1.ClearCachedDataSource()
                Throw New ArgumentException("กรุณาตรวจสอบ รหัสครุภัณฑ์")
            End If
        Else
            Dim check As Integer = 0
            Dim adap2 As New BasicDataTableAdapters.ActivityTypesTableAdapter
            check = adap2.CheckActType(e.Row.Cells(4).Text.ToString.Trim, Val(PlanYearsTxt.Text), Val(e.Row.Cells(3).Value))
            If check <> 0 Then
                Dim adaptype As New ActTypeDSTableAdapters.ActivityTypesTableAdapter
                Dim cmmtitem As String = ""
                cmmtitem = adaptype.GetCmmt(e.Row.Cells(4).Text.ToString.Trim, Val(PlanYearsTxt.Text))
                'MsgBox(If((pubprj(e.Row.Cells(2).Value) <> Nothing), Nothing, e.Row.Cells(1).Value))
                For Each item In pubprj
                    MsgBox(item.value)
                Next
                Try
                    id = adapter.InsertQuery(Val(PlanYearsTxt.Text), Val(PlanYearsTxt.Text), VersionTxt.Text, Nothing, Nothing, Val(e.Row.Cells(3).Value), e.Row.Cells(12).Text.ToString, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Val(e.Row.Cells(11).Value), Val(e.Row.Cells(6).Value), Val(e.Row.Cells(7).Value), Val(e.Row.Cells(9).Value), e.Row.Cells(13).Value, "", "F04", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Date.Now, Date.Now, Session("users"), Nothing, Val(PlanYearsTxt.Text), e.Row.Cells(4).Text, Nothing, Nothing, Nothing, Nothing, e.Row.Cells(1).Value, Val(PlanYearsTxt.Text), e.Row.Cells(2).Value, Val(PlanYearsTxt.Text), cmmtitem, Val(PlanYearsTxt.Text), Nothing, "2100", Val(PlanYearsTxt.Text), 0, If(pfctr = "null", Nothing, e.Row.Cells(1).Value))
                    logger.Info("User " & Session("users") & " insert activity type " & e.Row.Cells(4).Value & " of fund center " & e.Row.Cells(1).Value)
                Catch ex As Exception
                    logger.Error(ex.Message)
                    logger.Error(ex.StackTrace)

                    WebGrid1.ClearCachedDataSource()
                    Throw New ArgumentException("ไม่สามารถเพิ่มข้อมูลได้ กรุณาตรวจสอบจำนวนเงิน")
                End Try
            Else
                WebGrid1.ClearCachedDataSource()
                Throw New ArgumentException("กรุณาตรวจสอบ รหัสครุภัณฑ์")
            End If
        End If
        'adapter.InsertQuery1(2556)
        Dim dr As DataRow = e.Row.DataRow
        dr("Id") = id
        dr("Attachments_amount") = 0

        'Add By Orn 25/06/2558
        Dim adapterT As New BasicDataTableAdapters.ActivityTypesTemplateTableAdapter
        Dim dtTableT As New BasicData.ActivityTypesTemplateDataTable
        dtTableT = adapterT.GetDataTemplate(e.Row.Cells(4).Value, PlanYearsTxt.Text)
        If dtTableT.Rows.Count > 0 Then
            dr("TemplateName") = dtTableT.Rows(0)("TemplateName")
            dr("ActivityType_ActivityTypeCode") = dtTableT.Rows(0)("ActivityTypeCode")
            dr("PlanYear") = dtTableT.Rows(0)("PlanYear")
        End If
        If pfctr <> "null" Then
            dr("Project_ProjectCode") = dr("Project_ProjectCode") & "#" & dr("Fundcenter_FundcenterCode")
        End If
        If WebGrid1.RootTable.Rows.Count Mod 10 = 0 Then
            WebGrid1.ClientAction.Refresh()
        End If
    End Sub

    Protected Sub WebGrid1_UpdateRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        If lockhash(e.Row.Cells(1).Value.ToString.Trim) = True Then
            WebGrid1.ClearCachedDataSource()
            Throw New ArgumentException("ไม่สามารถบันทึกได้ เนื่องจากมีการส่งแผนไปแล้ว")
        End If

        Dim id As Decimal
        id = e.Row.KeyValue
        'WebGrid1.ClientAction.Alert(id)
        'Dim adapter2 As New BasicDataTableAdapters.CmmtDetailsTableAdapter
        'Dim cmmt_id As Decimal = adapter2.GetCmmtId(Val(e.Row.Cells(2).Value))

        Dim cmmt_id As String = "0"
        Dim asg As Integer = Val(e.Row.Cells(3).Value)
        Try
            Select Case asg
                Case 1
                    cmmt_id = cmmthash("ASSET_GROUP1_CMMT_CODE")
                Case 2
                    cmmt_id = cmmthash("ASSET_GROUP2_CMMT_CODE")
                Case 3
                    cmmt_id = cmmthash("ASSET_GROUP3_CMMT_CODE")
                Case 4
                    cmmt_id = cmmthash("ASSET_GROUP4_CMMT_CODE")
                Case 5
                    cmmt_id = cmmthash("ASSET_GROUP5_CMMT_CODE")
                Case 6
                    cmmt_id = cmmthash("ASSET_GROUP6_CMMT_CODE")
                Case 7
                    cmmt_id = cmmthash("ASSET_GROUP7_CMMT_CODE")
                Case 8
                    cmmt_id = cmmthash("ASSET_GROUP8_CMMT_CODE")
                Case 9
                    cmmt_id = cmmthash("ASSET_GROUP9_CMMT_CODE")
                Case 10
                    cmmt_id = cmmthash("ASSET_GROUP10_CMMT_CODE")

            End Select
        Catch ex As Exception
            logger.Error(ex.Message)
            logger.Error(ex.StackTrace)

            WebGrid1.ClearCachedDataSource()
            Throw New ArgumentException("ไม่สามารถบันทึกได้ เนื่องจากค่าในระบบไม่ถูกต้องโปรดติดต่อผู้ดูแลระบบ")
        End Try
        If cmmt_id = "0" Or cmmt_id Is Nothing Then
            WebGrid1.ClearCachedDataSource()
            Throw New ArgumentException("ไม่สามารถบันทึกได้ เนื่องจากค่าในระบบไม่ถูกต้องโปรดติดต่อผู้ดูแลระบบ")
        End If

        Dim padap As New BasicDataTableAdapters.ProjectsTableAdapter
        Dim pfctr As String = padap.GetFCTR(e.Row.Cells(2).Value, Val(PlanYearsTxt.Text))
        Dim sharpstr As Integer = InStr(e.Row.Cells(2).Value.ToString, "#")
        Dim projcode As String
        If sharpstr > 0 Then
            projcode = e.Row.Cells(2).Value.ToString.Remove(sharpstr - 1)
        Else
            projcode = e.Row.Cells(2).Value.ToString()
        End If
        Dim adapter As New BudgetDetailTableAdapters.BudgetDetailsTableAdapter
        Dim adapter12 As New SplitData2TableAdapters.Budget12TableAdapter
        If e.Row.Cells(4).Text.ToString.Trim.Remove(1) = "9" Then
            Dim adap3 As New ActTypeDSTableAdapters.BudgetDetailsTableAdapter
            Dim dup As Integer = 0
            dup = adap3.CheckAssetNo(e.Row.Cells(4).Text.ToString.Trim, e.Row.Cells(1).Value, Val(PlanYearsTxt.Text), id, VersionTxt.Text)
            If dup = 0 Then
                Try
                    adapter.UpdateATproject2(Val(e.Row.Cells(6).Value), Val(e.Row.Cells(7).Value), Val(e.Row.Cells(9).Value), Val(e.Row.Cells(11).Value), e.Row.Cells(13).Value, Date.Now, Session("users"), e.Row.Cells(1).Value, projcode, If(pfctr = "null", Nothing, e.Row.Cells(1).Value), Nothing, Nothing, e.Row.Cells(4).Text, e.Row.Cells(5).Text, e.Row.Cells(12).Value, Val(e.Row.Cells(3).Value), cmmt_id, Val(PlanYearsTxt.Text), id)
                    adapter12.DeleteBID(id)
                    logger.Info("User " & Session("users") & " update activity type " & e.Row.Cells(4).Value & " of fund center " & e.Row.Cells(1).Value)
                Catch ex As Exception
                    logger.Error(ex.Message)
                    logger.Error(ex.StackTrace)

                    WebGrid1.ClearCachedDataSource()
                    Throw New ArgumentException("ไม่สามารถแก้ไขข้อมูลได้ กรุณาตรวจสอบจำนวนเงิน")
                End Try
            Else
                WebGrid1.ClientAction.Refresh()
                Throw New ArgumentException("กรุณาตรวจสอบ รหัสครุภัณฑ์")
            End If

        Else
            Dim check As Integer = 0
            Dim adap2 As New BasicDataTableAdapters.ActivityTypesTableAdapter
            check = adap2.CheckActType(e.Row.Cells(4).Text, Val(PlanYearsTxt.Text), Val(e.Row.Cells(3).Value))
            If check <> 0 Then
                Dim adaptype As New ActTypeDSTableAdapters.ActivityTypesTableAdapter
                Dim cmmtitem As String = ""
                cmmtitem = adaptype.GetCmmt(e.Row.Cells(4).Text.ToString.Trim, Val(PlanYearsTxt.Text))
                Try
                    adapter.UpdateATproject1(Val(e.Row.Cells(6).Value), Val(e.Row.Cells(7).Value), Val(e.Row.Cells(9).Value), Val(e.Row.Cells(11).Value), e.Row.Cells(13).Value, Date.Now, Session("users"), e.Row.Cells(1).Value, projcode, If(pfctr = "null", Nothing, e.Row.Cells(1).Value), e.Row.Cells(4).Text, Val(PlanYearsTxt.Text), "", "", e.Row.Cells(12).Value, Val(e.Row.Cells(3).Value), cmmtitem, Val(PlanYearsTxt.Text), id)
                    adapter12.DeleteQuery(id)
                    logger.Info("User " & Session("users") & " update activity type " & e.Row.Cells(4).Value & " of fund center " & e.Row.Cells(1).Value)
                Catch ex As Exception
                    logger.Error(ex.Message)
                    logger.Error(ex.StackTrace)

                    WebGrid1.ClearCachedDataSource()
                    Throw New ArgumentException("ไม่สามารถแก้ไขข้อมูลได้ กรุณาตรวจสอบจำนวนเงิน")
                End Try
            Else
                WebGrid1.ClientAction.Refresh()
                Throw New ArgumentException("กรุณาตรวจสอบ รหัสครุภัณฑ์")
            End If

        End If
        Dim dr As DataRowView = e.Row.DataRow

        'Add By Orn 25/06/2558
        Dim adapterT As New BasicDataTableAdapters.ActivityTypesTemplateTableAdapter
        Dim dtTableT As New BasicData.ActivityTypesTemplateDataTable
        dtTableT = adapterT.GetDataTemplate(e.Row.Cells(4).Value, PlanYearsTxt.Text)
        If dtTableT.Rows.Count > 0 Then
            dr("TemplateName") = dtTableT.Rows(0)("TemplateName")
            dr("ActivityType_ActivityTypeCode") = dtTableT.Rows(0)("ActivityTypeCode")
            dr("PlanYear") = dtTableT.Rows(0)("PlanYear")
        End If

        If pfctr <> "null" Then
            dr("Project_ProjectCode") = projcode & "#" & dr("Fundcenter_FundcenterCode")
        End If
    End Sub


    Sub PlotNode()

        Dim MyNode As WebTreeViewNode
        MyNode = New WebTreeViewNode()

        Dim adapter As New FundCenterParentTableAdapters.fctr_viewTableAdapter
        Dim dt As New FundCenterParent.fctr_viewDataTable
        dt = adapter.GetDataByUser(Session("users"), Val(PlanYearsTxt.Text))

        Dim root_id As String

        MyNode.Text = "GSB"
        MyNode.Value = "0"
        root_id = "0"

        If dt.Count > 0 Then

            WebTreeView3.Nodes.Add(MyNode)

            Dim rootpath = ""
            Dim prefil As String = ""
            prefil = Session("prefilter")
            rootpath = FctrClass.GetRootPrefil(Session("users"), Val(Session("planyear")), prefil)
            Dim rootarr() As String = Split(rootpath, ",")
            WebTreeView3.Nodes.Add(MyNode)

            For i = 0 To rootarr.Length - 1
                PlotChild(rootarr(i), MyNode, 1)
            Next
        End If

    End Sub

    Sub PlotChild(ByVal gpath As String, ByRef cnode As WebTreeViewNode, ByVal level As Decimal)
        'Response.Write(gpath & "<br>")
        Dim Hadapter As New FundCenterParentTableAdapters.FctHTableAdapter
        Dim Hdatatable As New FundCenterParent.FctHDataTable
        Hdatatable = Hadapter.GetDataByGPath(gpath, Val(Session("planyear")))

        Dim MyNode As WebTreeViewNode
        MyNode = New WebTreeViewNode()
        For Each row As FundCenterParent.FctHRow In Hdatatable


            MyNode.Text = row.Description
            MyNode.Value = row.FundCenter_FundCenterCode

            cnode.Nodes.Add(MyNode)
        Next


        If Len(gpath) >= 10 Then
            Exit Sub
        End If

        Dim hadap2 As New FundCenterParentTableAdapters.UserLikeGpathTableAdapter
        Dim hdt2 As New FundCenterParent.UserLikeGpathDataTable
        Dim sql As String = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")

        Dim pathadd As String = "__"
        'For i = 1 To 4
        sql = ""
        Dim fullpath As String = gpath & pathadd
        If prefil <> "" Then
            sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
        End If
        sql += " AND grouppath like '" & fullpath & "' "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND Fcth.PlanYear = " & Val(Session("PlanYear"))
        hadap2.GetDataByFilter(hdt2, sql)

        For Each row As FundCenterParent.UserLikeGpathRow In hdt2
            If Len(row.grouppath) <= 10 Then
                PlotChild(row.grouppath, MyNode, level + 1)
            End If

        Next

    End Sub

    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click
        GenerateValues()
        If TabContainer1.ActiveTab.ID = TabPanel1.ID Then
            Session.Remove("search_fctr")
            If WebCombo1.Value <> "" And WebCombo2.Value <> "" Then
                Session.Add("search_fctr", FctrClass.GetFromToFundCenter(WebCombo1.Value, WebCombo2.Value, Session("users"), Val(PlanYearsTxt.Text)))
                'Response.Write(FctrClass.GetFromToFundCenter(WebCombo1.Value, WebCombo2.Value, Session("users")))
            ElseIf WebCombo1.Value <> "" And WebCombo2.Value = "" Then
                Session.Add("search_fctr", FctrClass.GetFromToFundCenter(WebCombo1.Value, WebCombo1.Value, Session("users"), Val(PlanYearsTxt.Text)))
                'Response.Write(FctrClass.GetFromToFundCenter(WebCombo1.Value, WebCombo1.Value, Session("users")))
            ElseIf WebCombo1.Value = "" And WebCombo2.Value <> "" Then
                Session.Add("search_fctr", FctrClass.GetFromToFundCenter(WebCombo2.Value, WebCombo2.Value, Session("users"), Val(PlanYearsTxt.Text)))
                'Response.Write(FctrClass.GetFromToFundCenter(WebCombo2.Value, WebCombo2.Value, Session("users")))
            Else
                Session.Add("search_fctr", Session("prefilter"))
            End If

            Session.Remove("search_activitytype")
            Dim activitytypeCriteria As String = ""
            If AssetGroupSearchCombo.Value.Trim <> "" Then
                activitytypeCriteria += " AND  ActivityTypes.AssetGroup = '" & AssetGroupSearchCombo.Value & "' "
            End If
            If ActivityTypeCodeSearchCombo.Value.Trim <> "" Then
                activitytypeCriteria += " AND  ActivityTypes.ActivityTypeCode = '" & ActivityTypeCodeSearchCombo.Value & "' "
            End If
            If ActivityTypeSearchCombo.Value.Trim <> "" Then
                activitytypeCriteria += " AND  ActivityTypes.Description = '" & ActivityTypeSearchCombo.Value & "' "
            End If
            Session.Add("search_activitytype", activitytypeCriteria)

        ElseIf TabContainer1.ActiveTab.ID = TabPanel2.ID Then
            Session.Remove("search_fctr")
            If WebCombo3.Value = "" Then
                Session.Add("search_fctr", Session("prefilter"))
            Else
                Session.Add("search_fctr", FctrClass.GetSubFundCenter(WebCombo3.Value, Session("users"), Session("Planyear"), Session("prefilter")))
            End If

            Session.Remove("search_activitytype")
            Dim activitytypeCriteria As String = ""
            If AssetGroupSearchCombo.Value.Trim <> "" Then
                activitytypeCriteria += " AND  ActivityTypes.AssetGroup = '" & AssetGroupSearchCombo.Value & "' "
            End If
            If ActivityTypeCodeSearchCombo.Value.Trim <> "" Then
                activitytypeCriteria += " AND  ActivityTypes.ActivityTypeCode = '" & ActivityTypeCodeSearchCombo.Value & "' "
            End If
            If ActivityTypeSearchCombo.Value.Trim <> "" Then
                activitytypeCriteria += " AND  ActivityTypes.Description = '" & ActivityTypeSearchCombo.Value & "' "
            End If
            Session.Add("search_activitytype", activitytypeCriteria)

        End If
        Session.Remove("Search")
        Session.Add("Search", "true")
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
        FundCenterCombo.ClearCachedDataSource()
        FundCenterCombo.DataBind()
    End Sub

    Sub GenerateValues()
        If hdf1.Value.Length = 0 Then
            Session("Search") = "false"
        End If
        Try
            Dim Numx As Array = hdf1.Value.Split(",")
            For i = 0 To Numx.Length - 1
                ms8.Items.FindByValue(Numx(i).ToString).Selected = True
            Next
        Catch ex As Exception
            logger.Error(ex.Message)
            logger.Error(ex.StackTrace)
        End Try

    End Sub


    'Protected Sub coppy_btn_Click(sender As Object, e As ImageClickEventArgs) Handles coppy_btn.Click

    '    Dim checkedRows As ArrayList = WebGrid1.RootTable.GetCheckedRows()

    '    Dim adapter As New fundCenterTableAdapters.BudgetDetailsTableAdapter

    '    For Each keyValue As String In checkedRows

    '        adapter.CoppyRow(keyValue)

    '    Next keyValue

    '    WebGrid1.ClearCachedDataSource()
    '    WebGrid1.RebindDataSource()

    'End Sub

    'Protected Sub delete_btn_Click(sender As Object, e As ImageClickEventArgs) Handles delete_btn.Click

    '    Dim checkedRows As ArrayList = WebGrid1.RootTable.GetCheckedRows()

    '    Dim adapter As New fundCenterTableAdapters.BudgetDetailsTableAdapter

    '    For Each keyValue As String In checkedRows

    '        adapter.DeleteRow(keyValue)

    '    Next keyValue



    '    WebGrid1.ClearCachedDataSource()
    '    WebGrid1.RebindDataSource()

    'End Sub

    Protected Sub WebGrid1_InitializePostBack(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.PostbackEventArgs)
        Try
            If e.Action = PostBackAction.Custom Then
                Dim ActionType As String = TryCast(Request("type"), String)
                If ActionType = "1" Then
                    Dim checkedRows As ArrayList = WebGrid1.RootTable.GetCheckedRows()

                    Dim adapter As New fundCenterTableAdapters.BudgetDetailsTableAdapter

                    For Each keyValue As String In checkedRows

                        adapter.CoppyRow(keyValue)

                    Next keyValue
                Else
                    Dim adapter As New fundCenterTableAdapters.BudgetDetailsTableAdapter
                    Dim adapter2 As New fundCenterTableAdapters.AttachmentsTableAdapter
                    Dim adapter3 As New AssetGenTableAdapters.FundedProgramAssetsTableAdapter
                    Dim adapter4 As New B06FPrgPD_DatasetTableAdapters.FundedProgramProjectsTableAdapter
                    For Each keyValue As String In WebGrid1.RootTable.GetCheckedRows()
                        Try
                            AttachClass.DeleteSP(keyValue)
                        Catch ex As Exception

                        End Try
                        adapter2.DeleteAttach(keyValue)
                        adapter3.DeleteBudgetId(keyValue)
                        adapter4.DeleteBudgetDetail(keyValue)
                        adapter.DeleteRow(keyValue)
                        logger.Info("User " & Session("users") & " delete activity type key " & keyValue)
                    Next keyValue
                End If

                WebGrid1.ClientAction.Refresh()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Protected Sub WebCombo1_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles WebCombo1.InitializeDataSource
        'If Session("Versions") > 1 Then
        'ds = PublicFunction.GetChild2(Session("users"))
        'ds.Tables(0).DefaultView.Sort = "FundCenterCode ASC"
        'e.DataSource = ds
        Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
        Dim dt As New BasicData.FCTR_Child2DataTable
        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYear"))
        If prefil <> "" Then
            sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
        End If
        sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("PlanYear")) & " "
        sql += "ORDER BY FundCenterCode ASC"
        adap.GetDataByFilter(dt, sql)
        dt.Rows.Add("", "ทุกรายการ", "1", "0")
        dt.DefaultView.Sort = "FundCenterCode"
        e.DataSource = dt

        'Else
        'Dim adap As New BasicDataTableAdapters.FCTR_ChildTableAdapter
        'Dim dt As New BasicData.FCTR_ChildDataTable
        'dt = adap.GetOwnData(Session("FCTR"))
        'e.DataSource = dt
        'End If
    End Sub

    Protected Sub WebCombo2_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles WebCombo2.InitializeDataSource
        'If Session("Versions") > 1 Then
        'ds = PublicFunction.GetChild2(Session("users"))
        'ds.Tables(0).DefaultView.Sort = "FundCenterCode ASC"
        'e.DataSource = ds
        Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
        Dim dt As New BasicData.FCTR_Child2DataTable
        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYear"))
        If prefil <> "" Then
            sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
        End If
        sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("PlanYear")) & " "
        sql += "ORDER BY FundCenterCode ASC"
        adap.GetDataByFilter(dt, sql)
        dt.Rows.Add("", "ทุกรายการ", "1", "0")
        dt.DefaultView.Sort = "FundCenterCode"
        e.DataSource = dt


        'Else
        'Dim adap As New BasicDataTableAdapters.FCTR_ChildTableAdapter
        'Dim dt As New BasicData.FCTR_ChildDataTable
        'dt = adap.GetOwnData(Session("FCTR"))
        'e.DataSource = dt
        'End If
    End Sub

    Protected Sub SaveBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SaveBtn.Click
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
    End Sub

    Protected Sub WebGrid1_InitializeColumn(sender As Object, e As ColumnEventArgs) Handles WebGrid1.InitializeColumn
        If e.Column.ColumnType = ColumnType.CheckBox And lockhash.Count > 0 Then
            e.Column.IsRowChecker = False
            e.Column.Width = 24
            e.Column.Caption = " "
        End If
        If Request("ptype") = "P" Then
            If e.Column.Name = "Project" Then
                e.Column.Caption = "โครงการ"
            End If
        Else
            If e.Column.Name = "Project" Then
                e.Column.Caption = "งานสนับสนุน"
            End If
        End If
    End Sub


    Protected Sub WebCombo3_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles WebCombo3.InitializeDataSource

        Dim adap As New FundCenterParentTableAdapters.SelectorTableAdapter
        Dim dt As New FundCenterParent.SelectorDataTable
        Dim dt2 As New FundCenterParent.SelectorDataTable
        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FctH.PlanYear = " & Val(Session("PlanYear"))
        If prefil <> "" Then
            sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
        End If
        sql += "ORDER BY Grouppath"
        adap.GetDataByFilter(dt2, sql)
        dt.Rows.Add("ทุกรายการ", "0", 0, "0", Val(Session("PlanYear")), "ทุกรายการ")
        dt.Merge(dt2)
        'dt.DefaultView.Sort = "FundCenter_FundCenterCode"
        e.DataSource = dt



    End Sub

    Protected Sub FundCenterCombo_ValueChanged(sender As Object, e As ISNet.WebUI.WebCombo.ValueChangedEventArgs) Handles FundCenterCombo.ValueChanged
        Dim adapA As New BasicDataTableAdapters.ActivityTypesTableAdapter
        Dim dtA As New BasicData.ActivityTypesDataTable


        dtA = adapA.GetDataByFundCenter(Val(Session("PlanYear")), e.Value)
        ActivityTypeCodeCombo.DataSource = dtA
        ActivityTypeCodeCombo.DataBind()

        ActivityTypeCombo.DataSource = dtA
        ActivityTypeCombo.DataBind()

   
        Dim adap As New BasicDataTableAdapters.Projects1TableAdapter
        Dim dt As New BasicData.Projects1DataTable
        dt = adap.GetDataByPrefix(Val(Session("PlanYear")), Request("ptype"))


        Dim prefil As String = Session("prefilter")
        Dim sql = ""

        sql += " LEFT JOIN ProjectFundCenters AS ProjectFundCenters1 ON(Projects.ProjectCode = ProjectFundCenters1.ProjectCode AND Projects.PlanYear = ProjectFundCenters1.PlanYear) "
        sql += " WHERE 1 = 1 "
        sql += " AND Projects.FundCenter_FundCenterCode IS NULL  "
        sql += " AND Projects.PlanYear = " & PlanYearsTxt.Text & " "
        sql += " AND LEFT(Projects.ProjectCode,1) = '" & Request("ptype") & "' "
        sql += " AND ProjectFundCenters1.ProjectCode IS NULL "

        sql += " UNION "

        sql += " SELECT        Projects.ProjectCode, Projects.Description, Projects.FundCenter_FundCenterCode AS FundCenterCode "
        sql += " FROM Projects "
        sql += " INNER JOIN ProjectFundCenters AS ProjectFundCenters1 ON(Projects.ProjectCode = ProjectFundCenters1.ProjectCode AND Projects.PlanYear = ProjectFundCenters1.PlanYear) "
        sql += " WHERE 1 = 1 "
        sql += " AND Projects.FundCenter_FundCenterCode IS NULL "
        sql += " AND Projects.PlanYear = " & PlanYearsTxt.Text & " "
        sql += " AND LEFT(Projects.ProjectCode,1) = '" & Request("ptype") & "' "
        sql += " AND CONVERT(INT,'" & e.Value & "') BETWEEN CONVERT(INT,ProjectFundCenters1.FromFundCenter_FundCenterCode) AND CONVERT(INT,ProjectFundCenters1.TOFundCenter_FundCenterCode) "



        Dim dt3 As New BasicData.Projects1DataTable
        adap.GetDataByFilter(dt3, sql)

        prefil = Session("prefilter")
        Dim fadap As New FormLockDSTableAdapters.FundCentersTableAdapter
        Dim fdt As New FormLockDS.FundCentersDataTable
        Dim pfadp As New B10DataSetTableAdapters.ProjectFundCentersTableAdapter
        fadap.GetDataByFilter(fdt, prefil)
        For Each fitem As FormLockDS.FundCentersRow In fdt
            Dim isMerge = False
            For Each item As BasicData.Projects1Row In dt3

                item.FundCenterCode = fitem.FundCenter_FundCenterCode

            Next

            dt.Merge(dt3)

        Next

        ProjectCombo.DataSource = dt
        ProjectCombo.DataBind()

    End Sub

    Protected Sub AssetGroupSearchCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles AssetGroupSearchCombo.InitializeDataSource

        Dim adap As New DBdataTableAdapters.AssetGroupTableAdapter
        Dim dt As New DBdata.AssetGroupDataTable
        dt = adap.GetData

        e.DataSource = dt
    End Sub

    Protected Sub ActivityTypeCodeSearchCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles ActivityTypeCodeSearchCombo.InitializeDataSource
        Dim adap As New BasicDataTableAdapters.ActivityTypesTableAdapter
        Dim dt As New BasicData.ActivityTypesDataTable
        dt = adap.GetDataByPlanYear(Val(Session("PlanYear")))
        e.DataSource = dt
    End Sub

    Protected Sub ActivityTypeSearchCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles ActivityTypeSearchCombo.InitializeDataSource
        Dim adap As New BasicDataTableAdapters.ActivityTypesTableAdapter
        Dim dt As New BasicData.ActivityTypesDataTable
        dt = adap.GetDataByPlanYear(Val(Session("PlanYear")))
        e.DataSource = dt
    End Sub
End Class
